<?php
namespace app\common\model;
use think\Db;
/**
 * 自定义标签模型
 */
class Tags extends Base
{
	/**
	 * 单数据库查询
	 */
	public function zhtDb($table, $where, $order, $field, $num, $cache)
	{
		$cacheData = cache('TAG_TABLE_' . $table . "_" . $field . "_" . $num . "_" . $cache);
		if ($cacheData) return $cacheData;
		$rs = Db::name($table)->field($field)
			->where($where)
			->order($order)
			->limit($num)
			->select();
		cache('TAG_TABLE_' . $table . "_" . $field . "_" . $num . "_" . $cache, $rs, $cache);
		return $rs;
	}
	/**
	 * 获取指定产品
	 */
	public function listProduct($type, $catId = 0, $num, $cache = 0)
	{
		$cacheData = cache('TAG_PRODUCT_' . $type . "_" . $catId . "_" . $num . "_" . $cache);
		if ($cacheData) return $cacheData;
		//检测是否有数据
		$where = [];
		$where[] = ['isShow', '=', 1];
		$where[] = ['dataFlag', '=', 1];
		if($catId != 0){
			//获取子集
			$ids = array();
			$ids[] = $catId;
			$ids = model('product_cat')->getChild($ids,$ids);
			$where[] = ['catId','in',$ids];
		}
		// if($catId > 0)$where[] = ['catId', '=', $catId];
		if($type != ''){
			$order = ''.$type.' desc,dataSort desc,productId desc';
		}else{
			$order = 'dataSort desc,productId desc';
		}
		$product = Db::name('product')
			->where($where)
			->field('productId,productTitle,productImg,productContent,create_time')
			->order($order)
			->limit($num)
			->select();
		// dump($product);
		cache('TAG_PRODUCT_' . $type . "_" . $catId . "_" . $num . "_" . $cache, $product, $cache);
		return $product;
	}
	/**
	 * 获取指定文章
	 */
	public function listArticle($type, $catId = 0, $num, $cache = 0)
	{
		$cacheData = cache('TAG_ARTICLE_' . $type . "_" . $catId . "_" . $num . "_" . $cache);
		if ($cacheData) return $cacheData;
		//检测是否有数据
		$where = [];
		$where[] = ['isShow', '=', 1];
		$where[] = ['dataFlag', '=', 1];
		if($catId > 0)$where[] = ['catId', '=', $catId];
		if($type != ''){
			$order = ''.$type.' desc,dataSort desc,articleId desc';
		}else{
			$order = 'dataSort desc,articleId desc';
		}
		$article = Db::name('article')
			->where($where)
			->field('articleId,articleTitle,articleImg,articleContent,create_time')
			->order($order)
			->limit($num)
			->select();
		// dump($article);
		cache('TAG_ARTICLE_' . $type . "_" . $catId . "_" . $num . "_" . $cache, $article, $cache);
		return $article;
	}
	/**
	 * 获取指定单页
	 */
	public function listAbout($type, $catId = 0, $num, $cache = 0)
	{
		$cacheData = cache('TAG_ABOUT_' . $type . "_" . $catId . "_" . $num . "_" . $cache);
		if ($cacheData) return $cacheData;
		//检测是否有数据
		$where = [];
		$where[] = ['isShow', '=', 1];
		$where[] = ['dataFlag', '=', 1];
		if($catId > 0)$where[] = ['catId', '=', $catId];
		$order = 'dataSort desc,aboutId asc';
		$about = Db::name('about')
			->where($where)
			->field('aboutId,aboutTitle,aboutTitle2,aboutImg,aboutContent')
			->order($order)
			->limit($num)
			->select();
		// dump($about);
		cache('TAG_ABOUT_' . $type . "_" . $catId . "_" . $num . "_" . $cache, $about, $cache);
		return $about;
	}
	/**
	 * 获取指定附加项
	 */
	public function listOther($type, $catId = 0, $num, $cache = 0)
	{
		$cacheData = cache('TAG_OTHER_' . $type . "_" . $catId . "_" . $num . "_" . $cache);
		if ($cacheData) return $cacheData;
		//检测是否有数据
		$where = [];
		$where[] = ['isShow', '=', 1];
		$where[] = ['dataFlag', '=', 1];
		if($catId > 0)$where[] = ['catId', '=', $catId];
		$order = 'dataSort desc,otherId asc';
		$other = Db::name('other')
			->where($where)
			->field('otherId,otherTitle,otherTitle2,otherImg,otherContent')
			->order($order)
			->limit($num)
			->select();
		// dump($other);
		cache('TAG_OTHER_' . $type . "_" . $catId . "_" . $num . "_" . $cache, $other, $cache);
		return $other;
	}
	/**
	 * 获取指定案例
	 */
	public function listCases($type, $catId = 0, $num, $cache = 0)
	{
		$cacheData = cache('TAG_CASES_' . $type . "_" . $catId . "_" . $num . "_" . $cache);
		if ($cacheData) return $cacheData;
		//检测是否有数据
		$where = [];
		$where[] = ['isShow', '=', 1];
		$where[] = ['dataFlag', '=', 1];
		if($catId > 0)$where[] = ['catId', '=', $catId];
		if($type != ''){
			$order = ''.$type.' desc,dataSort desc,casesId desc';
		}else{
			$order = 'dataSort desc,casesId desc';
		}
		$cases = Db::name('cases')
			->where($where)
			->field('casesId,casesTitle,casesImg,casesContent,create_time')
			->order($order)
			->limit($num)
			->select();
		// dump($cases);
		cache('TAG_CASES_' . $type . "_" . $catId . "_" . $num . "_" . $cache, $cases, $cache);
		return $cases;
	}
	/**
	 * 获取指定视频
	 */
	public function listVideo($type, $catId = 0, $num, $cache = 0)
	{
		$cacheData = cache('TAG_VIDEO_' . $type . "_" . $catId . "_" . $num . "_" . $cache);
		if ($cacheData) return $cacheData;
		//检测是否有数据
		$where = [];
		$where[] = ['dataFlag', '=', 1];
		if($catId > 0)$where[] = ['catId', '=', $catId];
		if($type != ''){
			$order = ''.$type.' desc,dataSort desc,videoId desc';
		}else{
			$order = 'dataSort desc,videoId desc';
		}
		$video = Db::name('video')
			->where($where)
			->field('videoId,videoTitle,videoImg,videoUrl')
			->order($order)
			->limit($num)
			->select();
		// dump($video);
		cache('TAG_VIDEO_' . $type . "_" . $catId . "_" . $num . "_" . $cache, $video, $cache);
		return $video;
	}
	/**
	 * 获取指定商品
	 */
	public function listGoods($type, $catId = 0, $num, $cache = 0)
	{
		$type = strtolower($type);
		if (strtolower($type) == 'history') {
			return $this->historyByGoods($num);
		} elseif (strtolower($type) == 'guess') {
			return $this->getGuessLike($catId, $num);
		} else {
			return $this->listByGoods($type, $catId, $num, $cache);
		}
	}
	/**
	 * 获取指定商品-首页商品
	 */
	public function listGoodsindex($type, $catId = 0, $num, $cache = 0)
	{
		$cacheData = cache('TAG_GOODSINDEX_' . $type . "_" . $catId . "_" . $num . "_" . $cache);
		if ($cacheData) return $cacheData;
		//检测是否有数据
		$where = [];
		// $where[] = ['r.dataSrc', '=', 0];
		$where[] = ['g.isSale', '=', 1];
		$where[] = ['g.goodsStatus', '=', 1];
		$where[] = ['g.dataFlag', '=', 1];
		$goods = [];
		// if($type != '')$where[] = ['g.'.$type, '=', 1];
		if($type != ''){
			$order = 'g.'.$type.' desc,g.dataSort desc,g.goodsId desc';
		}else{
			$order = 'g.dataSort desc,g.goodsId desc';
		}
		//获取子集
		$ids = array();
		$ids[] = $catId;
		$ids = model('common/goodsCat')->getChild($ids,$ids);
		$where[] = ['g.catId', 'in', implode(',',$ids)];
		$goods = Db::name('goods')->alias('g')
			->join('__SHOP__ s', 'g.shopId=s.shopId')
			->join('__GOODS_CAT__ gc', 'g.catId=gc.catId')
			->where($where)
			->field('g.catId,g.goodsId,g.goodsTitle,g.shopPrice,g.goodsImg,s.shopId,s.shopTitle,gc.catTitle')
			->order($order)
			->limit($num)
			->select();
		// dump($goods);
		cache('TAG_GOODSINDEX_' . $type . "_" . $catId . "_" . $num . "_" . $cache, $goods, $cache);
		return $goods;
	}
	/**
	 * 获取指定商品-产业链商品
	 */
	public function listGoodschain($type, $catId = 0, $chainType = 0, $num, $cache = 0)
	{
		$cacheData = cache('TAG_GOODSCHAIN_' . $type . "_" . $catId . "_" . $chainType . "_" . $num . "_" . $cache);
		if ($cacheData) return $cacheData;
		//检测是否有数据
		$where = [];
		// $where[] = ['r.dataSrc', '=', 0];
		$where[] = ['g.isSale', '=', 1];
		$where[] = ['g.goodsStatus', '=', 1];
		$where[] = ['g.dataFlag', '=', 1];
		$where[] = ['c.chainType', '=', $chainType];
		$where[] = ['c.catId', '=', $catId];
		$goods = [];
		// if($type != '')$where[] = ['g.'.$type, '=', 1];
		if($type != ''){
			$order = 'g.'.$type.' desc,g.dataSort desc,g.goodsId desc';
		}else{
			$order = 'g.dataSort desc,g.goodsId desc';
		}
		$goods = Db::name('goods')->alias('g')
			->join('__SHOP__ s', 'g.shopId=s.shopId','left')
			->join('__GOODS_CAT__ gc', 'g.catId=gc.catId','left')
			->join('__CHAIN__ c', 'c.shopId=s.shopId','left')
			->where($where)
			->field('g.catId,g.goodsId,g.goodsTitle,g.shopPrice,g.goodsImg,s.shopId,s.shopTitle,gc.catTitle')
			->order($order)
			->limit($num)
			->select();
		// dump($goods);
		cache('TAG_GOODSCHAIN_' . $type . "_" . $catId . "_" . $chainType . "_" . $num . "_" . $cache, $goods, $cache);
		return $goods;
	}
	/**
	 * 猜你喜欢
	 * @param catId:分类id
	 * @param num:数据条数
	 */
	public function getGuessLike($catId, $num, $goodsIds = [])
	{
		$module = request()->module();
		if ($module == 'app' || $module == 'weapp') {
			$ids = $goodsIds;
		} else {
			$ids = ($module == 'home') ? cookie("history_goods") : (($module == 'mobile') ? cookie("mo_history_goods") : cookie("wx_history_goods"));
		}

		// 当前无浏览记录，取热销商品
		$where = [];
		$where[] = ['isSale', '=', 1];
		$where[] = ['goodsStatus', '=', 1];
		$where[] = ['dataFlag', '=', 1];
		$where[] = ['goodsId', 'in', $ids];
		$goods_group = Db::name('goods')
			->where($where)
			->group('catId')
			->column('catId');
		$goods = Db::name('goods')->field('goodsId,goodsTitle,goodsImg,goodsSn,goodsStock,saleNum,shopPrice,marketPrice,isSpec,appraiseNum,dataVisit,isNew')
			->where([['isSale', '=', 1], ['goodsStatus', '=', 1], ['dataFlag', '=', 1]])
			->where([['catId', 'in', $goods_group]])
			->limit(3 * $num)
			->select();

		if (empty($goods)) return $this->listByGoods('hot', $catId, $num);

		// 从数组中随机取$num个单元
		shuffle($goods);
		$goods = array_slice($goods, 0, $num);
		return $goods;
	}
	/**
	 * 浏览商品
	 */
	public function historyByGoods($num)
	{
		$hids = $ids = cookie("history_goods");
		if (empty($ids)) return [];
		$where = [];
		$where[] = ['isSale', '=', 1];
		$where[] = ['goodsStatus', '=', 1];
		$where[] = ['g.dataFlag', '=', 1];
		$where[] = ['goodsId', 'in', $ids];
		$orderBy = "field(`goodsId`," . implode(',', $ids) . ")";
		$goods = Db::name('goods')->alias('g')->join('__SHOPS__ s', 'g.shopId=s.shopId')
			->where($where)->field('s.shopTitle,s.shopId,goodsId,goodsTitle,goodsImg,goodsSn,goodsStock,saleNum,shopPrice,marketPrice,isSpec,appraiseNum,dataVisit')
			->limit($num)
			->orderRaw($orderBy)
			->select();
		$ids = [];
		foreach ($goods as $key => $v) {
			if ($v['isSpec'] == 1) $ids[] = $v['goodsId'];
		}
		if (!empty($ids)) {
			$specs = [];
			$rs = Db::name('goods_specs gs ')->where([['goodsId', 'in', $ids], ['dataFlag', '=', 1]])->order('id')->select();
			foreach ($rs as $key => $v) {
				$specs[$v['goodsId']] = $v;
			}
			foreach ($goods as $key => $v) {
				if (isset($specs[$v['goodsId']]))
					$goods[$key]['specs'] = $specs[$v['goodsId']];
			}
		}
		return $goods;
	}
	/**
	 * 推荐商品
	 */
	public function listByGoods($type, $catId, $num, $cache = 0)
	{
		if (!in_array($type, [0, 1, 2, 3])) return [];
		$cacheData = cache('TAG_GOODS_' . $type . "_" . $catId . "_" . $num . "_" . $cache);
		if ($cacheData) return $cacheData;
		//检测是否有数据
		$types = ['recom' => 0, 'new' => 3, 'hot' => 1, 'best' => 2];
		$where = [];
		// $where[] = ['r.dataSrc', '=', 0];
		$where[] = ['g.isSale', '=', 1];
		$where[] = ['g.goodsStatus', '=', 1];
		$where[] = ['g.dataFlag', '=', 1];
		$goods = [];
		if ($type != 'visit') {
			$where[] = ['r.dataType', '=', $types[$type]];
			$where[] = ['r.catId', '=', (int)$catId];
			$goods = Db::name('goods')->alias('g')
				// ->join('__RECOMMENDS__ r', 'g.goodsId=r.dataId')
				->join('__SHOP__ s', 'g.shopId=s.shopId')
				->where($where)
				->field('g.goodsTips,s.shopTitle,s.shopId,g.goodsId,goodsTitle,goodsImg,goodsSn,goodsStock,saleNum,shopPrice,marketPrice,isSpec,appraiseNum,dataVisit,isNew')
				->order('r.dataSort desc')->limit($num)->select();
		}
		//判断有没有设置，如果没有设置的话则获取实际的数据
		if (empty($goods)) {
			$goodsCatIds = ZHTGoodsCatPath($catId);
			$types = ['recom' => 'isRecom', 'new' => 'isNew', 'hot' => 'isHot', 'best' => 'isBest'];
			$order = [
				'recom' => 'saleNum desc,goodsId asc',
				'new' => 'saleTime desc,goodsId asc',
				'hot' => 'saleNum desc,goodsId asc',
				'best' => 'saleNum desc,goodsId asc',
				'visit' => 'dataVisit desc'
			];

			$where = [];
			$where[] = ['isSale', '=', 1];
			$where[] = ['goodsStatus', '=', 1];
			$where[] = ['g.dataFlag', '=', 1];

			if ($type != 'visit')
				$where[] = [$types[$type], '=', 1];



			if (!empty($goodsCatIds)) $where[] = ['g.goodsCatIdPath', 'like', implode('_', $goodsCatIds) . '\_%'];
			$goods = Db::name('goods')->alias('g')
				->join('__SHOP__ s', 'g.shopId=s.shopId')
				->where($where)
				->field('g.goodsTips,s.shopTitle,s.shopId,goodsId,goodsTitle,goodsImg,goodsSn,goodsStock,saleNum,shopPrice,marketPrice,isSpec,appraiseNum,dataVisit,isNew')
				->order($order[$type])->limit($num)->select();
		}
		$ids = [];
		foreach ($goods as $key => $v) {
			if ($v['isSpec'] == 1) $ids[] = $v['goodsId'];
		}
		if (!empty($ids)) {
			$specs = [];
			$rs = Db::name('goods_spec gs ')->where([['goodsId', 'in', $ids], ['dataFlag', '=', 1]])->order('id asc')->select();
			foreach ($rs as $key => $v) {
				$specs[$v['goodsId']] = $v;
			}
			foreach ($goods as $key => $v) {
				if (isset($specs[$v['goodsId']]))
					$goods[$key]['specs'] = $specs[$v['goodsId']];
			}
		}
		cache('TAG_GOODS_' . $type . "_" . $catId . "_" . $num . "_" . $cache, $goods, $cache);
		return $goods;
	}

	/**
	 * 获取广告位置
	 */
	public function listAds($catId, $num, $cache = 0, $sortType = 0)
	{
		$cacheData = cache('TAG_ADS' . $catId . "_" . $cache);
		if ($cacheData) return $cacheData;
		$today = date('Y-m-d');
		$order = ($sortType == 0) ? 'dataSort desc' : 'create_time desc';
		$rs = Db::name("ad")
			->where("dataFlag=1 and catId='" . $catId . "' and ((startDate<= '$today' and endDate>='$today') or (startDate<= '$today' and endDate is null) or (startDate is null and endDate>='$today') or (startDate is null and endDate is null))")
			->field('adId,adTitle,adTitle2,adUrl,adImg,adImg2')
			->order($order)->limit($num)->select();
		if (count($rs) > 0) {
			foreach ($rs as $key => $v) {
				$rs[$key]['isOpen'] = false;
				if (stripos($v['adUrl'], 'http:') !== false || stripos($v['adUrl'], 'https:') !== false) {
					$rs[$key]['isOpen'] = true;
				}
			}
		}
		cache('TAG_ADS' . $catId . "_" . $cache, $rs, $cache);
		return $rs;
	}
	/**
	 * 获取友情链接，合作伙伴
	 */
	public function listLink($catId, $num, $cache = 0)
	{
		$cacheData = cache('TAG_LINK' . $catId . "_" . $cache);
		if ($cacheData) return $cacheData;
		$rs = Db::name("link")
			->where("dataFlag=1 and catId='" . $catId . "'")
			->order("dataSort desc,linkId asc")
			->limit($num)
			->select();
		cache('TAG_LINK' . $catId . "_" . $cache, $rs, $cache);
		return $rs;
	}

	/**
	 * 产业链商机
	 */
	public function listBusiness($type = '',$catId = 0, $num, $cache = 0)
	{
		$cacheData = cache('TAG_BUSINESS_' . $type . "_" . $catId . "_" . $num . "_" . $cache);
		if ($cacheData) return $cacheData;
		$where = [];
		$where[] = ['dataFlag', '=', 1];
		if($catId > 0)$where[] = ['catId', '=', $catId];
		if($type != ''){
			$order = $type.' desc,dataSort desc,businessId desc';
		}else{
			$order = 'dataSort desc,businessId desc';
		}
		$rs = Db::name('business')->where($where)
			->field("businessId, businessTitle,create_time")
			->order($order)
			->limit($num)
			->select();
		// echo '垃'.$catId.'圾'.$goodscat;
		// die;
		cache('TAG_BUSINESS_' . $type . "_" . $catId . "_" . $num . "_" . $cache, $rs, $cache);
		return $rs;
	}
	/**
	 * 文档
	 */
	public function listWord($type = '',$catId = 0, $num, $cache = 0)
	{
		$cacheData = cache('TAG_WORD_' . $type . "_" . $catId . "_" . $num . "_" . $cache);
		if ($cacheData) return $cacheData;
		$where = [];
		$where[] = ['dataFlag', '=', 1];
		if($catId > 0)$where[] = ['catId', '=', $catId];
		if($type != ''){
			$order = $type.' desc,dataSort desc,wordId desc';
		}else{
			$order = 'dataSort desc,wordId desc';
		}
		$rs = Db::name('word')->where($where)
			->order($order)
			->limit($num)
			->select();
		// echo '垃'.$catId.'圾'.$goodscat;
		// die;
		cache('TAG_WORD_' . $type . "_" . $catId . "_" . $num . "_" . $cache, $rs, $cache);
		return $rs;
	}
	/**
	 * 软件
	 */
	public function listSoftware($type = '',$catId = 0, $num, $cache = 0)
	{
		$cacheData = cache('TAG_SOFTWARE_' . $type . "_" . $catId . "_" . $num . "_" . $cache);
		if ($cacheData) return $cacheData;
		$where = [];
		$where[] = ['dataFlag', '=', 1];
		if($catId > 0)$where[] = ['catId', '=', $catId];
		if($type != ''){
			$order = $type.' desc,dataSort desc,softwareId desc';
		}else{
			$order = 'dataSort desc,softwareId desc';
		}
		$rs = Db::name('software')->where($where)
			->order($order)
			->limit($num)
			->select();
		// echo '垃'.$catId.'圾'.$goodscat;
		// die;
		cache('TAG_SOFTWARE_' . $type . "_" . $catId . "_" . $num . "_" . $cache, $rs, $cache);
		return $rs;
	}
	/**
	 * 软件分类列表
	 */
	public function listSoftwareCats($parentId = 0, $num, $cache = 0){
		$cacheData = cache('TAG_SOFTWARE_CATS_' . $parentId . "_" . $num . "_" . $cache);
		if ($cacheData) return $cacheData;
		$where = [];
		$where[] = ['isShow', '=', 1];
		$where[] = ['dataFlag', '=', 1];
		$where[] = ['parentId', '=', $parentId];
		$data = Db::name('software_cat')
			->field('catId,catTitle')
			->limit($num)->where($where)
			->order('dataSort desc')
			->select();
		cache('TAG_SOFTWARE_CATS_' . $parentId . "_" . $num . "_" . $cache, $data, $cache);
		return $data;
	}
	/**
	 * 获取最新文章
	 */
	public function listByNewArticle($num, $cache)
	{
		$cacheData = cache('TAG_NEW_ARTICLES_' . $cache);
		if ($cacheData) return $cacheData;
		$rs = Db::name('articles')->alias('a')->field('a.articleId,a.articleTitle,a.coverImg,a.create_time')->join('article_cats ac', 'a.catId=ac.catId', 'inner')
			->where('ac.catType=0 and ac.parentId<>7 and a.dataFlag=1 and ac.isShow=1 and a.isShow=1 and ac.dataFlag=1')->order('a.dataSort desc,a.create_time desc')->limit($num)->select();
		cache('TAG_NEW_ARTICLES_' . $cache, $rs, $cache);
		return $rs;
	}
	/**
	 * 获取指定分类的文章
	 */
	public function listByArticle($catId, $num, $cache)
	{
		$where = [];
		$where[] = ['dataFlag', '=', 1];
		$where[] = ['isShow', '=', 1];
		if (is_array($catId)) {
			$where[] = ['catId', 'in', $catId];
		} else {
			$where[] = ['catId', '=', $catId];
		}
		return Db::name('articles')->where($where)
			->field("articleId, catId, articleTitle,articleContent,coverImg,create_time")->order('dataSort desc,create_time desc')->limit($num)->select();
	}

	/**
	 * 获取分类下的品牌
	 */
	public function listBrand($catId, $num, $cache = 0)
	{
		$cacheData = cache('TAG_BRANDS_' . $catId . "_" . $cache);
		if ($cacheData) return $cacheData;
		$where = [];
		$where[] = ['r.dataSrc', '=', 2];
		$where[] = ['b.dataFlag', '=', 1];
		$where[] = ['r.dataType', '=', 0];
		$where[] = ['r.catId', '=', $catId];
		$brands = Db::name('brands')->alias('b')->join('__RECOMMENDS__ r', 'b.brandId=r.dataId')
			->where($where)->field('b.brandId,b.brandImg,b.brandName,r.catId catId')
			->order('r.dataSort')->limit($num)->select();
		//为空的话就取分类关联的
		if (empty($brands)) {
			$where = [['b.dataFlag', '=', 1]];
			if ($catId > 0) {
				$where[] = ['gc.catId', '=', $catId];
				$brands = Db::name('goods_cats')->alias('gc')
					->join('__CAT_BRANDS__ gcb', 'gc.catId=gcb.catId', 'inner')
					->join('__BRANDS__ b', 'gcb.brandId=b.brandId and b.dataFlag=1', 'inner')
					->field('b.brandId,b.brandImg,b.brandName,gcb.catId')
					->group('b.brandId,gcb.catId')
					->where('gc.dataFlag=1 and gc.isShow=1')
					->where($where)
					->limit($num)
					->select();
			} else {
				$brands = Db::name('brands')->field('brandId,brandImg,brandName,0 catId')->where(['dataFlag' => 1])->order('sortNo asc')->limit($num)->select();
			}
		}
		$brands = $this->unique_multidim_array($brands, 'brandId');
		cache('TAG_BRANDS_' . $catId . "_" . $cache, $brands, $cache);
		return $brands;
	}
	// 二位数组去重
	protected function unique_multidim_array($array, $key)
	{
		$temp_array = array();
		$i = 0;
		$key_array = array();
		foreach ($array as $val) {
			if (!in_array($val[$key], $key_array)) {
				$key_array[$i] = $val[$key];
				$temp_array[$i] = $val;
			}
			$i++;
		}
		return $temp_array;
	}

	/**
	 * 获取分类下推荐的店铺
	 */
	public function listShop($catId, $num, $cache = 0)
	{
		$cacheData = cache('TAG_SHOP_' . $catId . "_" . $cache);
		if ($cacheData) return $cacheData;
		$where = [];
		$where[] = ['r.dataSrc', '=', 1];
		$where[] = ['s.dataFlag', '=', 1];
		$where[] = ['s.shopStatus','=',1];
		$where[] = ['s.applyStatus', '=', 2];
		$where[] = ['r.dataType', '=', 0];
		$where[] = ['r.catId', '=', $catId];
		$rs = Db::name('shop')->alias('s')
			->join('__RECOMMEND__ r', 's.shopId=r.dataId')
			->where($where)
			->field('s.shopId,s.shopImg,s.shopTitle')
			->order('r.dataSort', 'desc')
			->limit($num)
			->select();
		cache('TAG_SHOP_' . $catId . "_" . $cache, $rs, $cache);
		return $rs;
	}
	/**
	 * 获取分类下的店铺-产业链获取上下游企业
	 */
	public function listShopchain($catId, $chainType = 0,$num, $cache = 0)
	{
		$cacheData = cache('TAG_SHOP_INDEX_' . $catId . "_" . $chainType . "_" . $cache);
		if ($cacheData) return $cacheData;
		$where = [];
		$where[] = ['s.dataFlag', '=', 1];
		$where[] = ['s.shopStatus','=',1];
		$where[] = ['s.applyStatus', '=', 2];
		$where[] = ['c.dataFlag', '=', 1];
		$where[] = ['c.catId', '=', $catId];
		$where[] = ['c.chainType', '=', $chainType];
		$shops = Db::name('shop')->alias('s')
			->join('__CHAIN__ c', 's.shopId=c.shopId', 'inner')
			->where($where)
			->field('s.shopId,s.shopImg,s.shopTitle,s.shopTel,areaIdPath')
			->order('s.shopId desc')
			->limit($num)
			->select();
		$shopCatMap = model('home/shop')->shopCatMap(); // 获取所有店铺的经营类目，以shopId为主键
		foreach ($shops as $key => $v) {
			$shops[$key]['areaNames'] = model('home/area')->getAreaNames($v['areaIdPath']);
			$shops[$key]['shopcat'] = (isset($shopCatMap[$v['shopId']]))?implode(',',$shopCatMap[$v['shopId']]):'';
		}
		cache('TAG_SHOP_INDEX_' . $catId . "_" . $chainType . "_" . $cache, $shops, $cache);
		return $shops;
	}

	/**
	 * 获取订单列表
	 */
	public function listOrder($type, $num, $cache, $fields = '')
	{
		if (!in_array($type, ['user', 'shop'])) return [];
		$ownId = (int)($type == 'user') ? session('ZHT_USER.userId') : session('ZHT_USER.shopId');
		if ($ownId == 0) return [];
		if ($fields == '') $fields = 'orderId,orderNo,create_time,orderStatus,payType,deliverType,userName,realTotalMoney';
		$data = cache('TAG_ORDER_' . $type . "_" . $ownId . "_" . $cache);
		if (!$data) {
			$where = '';
			if ($type == 'user') $where = 'userId=' . $ownId;
			if ($type == 'shop') $where = 'shopId=' . $ownId;
			$db = Db::name('order')->where($where)->limit($num)->order('create_time', 'desc');
			if ($fields != '') $db->field($fields);
			$data = $db->select();
			if (!empty($data)) {
				$ids = [];
				foreach ($data as $key => $v) {
					$ids[] = $v['orderId'];
				}
				$goods = Db::name('order_goods')->where('orderId in (' . implode(',', $ids) . ')')->order('id', 'asc')->select();
				$goodsMap = [];
				foreach ($goods as $g) {
					$goodsMap[$g['orderId']][] = $g;
				}
				foreach ($data as $key => $v) {
					$data[$key]['goods'] = $goodsMap[$v['orderId']];
				}
			}
			cache('TAG_ORDER_' . $type . "_" . $ownId . "_" . $cache, $data, $cache);
		}
		return $data;
	}

	/**
	 * 获取收藏商品/商家列表
	 */
	public function listFavorite($type, $num, $fields = '')
	{
		if (!in_array($type, ['goods', 'shop'])) return [];
		$userId = (int)session('ZHT_USER.userId');
		if ($userId == 0) return [];
		$where = 'userId=' . $userId;
		if ($type == 'goods') {
			$db = Db::name('favorites')->alias('f');
			$db->join('__GOODS__ g', 'f.goodsId=g.goodsId and g.dataFlag=1 ');
			if ($fields == '') $fields = 'g.goodsId,g.goodsTitle,g.goodsImg,g.isSale,g.shopPrice';
			$db->field($fields);
			$db->limit($num)->where($where);
			$db->order('favoriteId desc');
			return $db->select();
		} else {
			$db = Db::name('shop_members')->alias('f');
			$db->join('__SHOPS__ s', 'f.shopId=s.shopId and s.dataFlag=1 and s.shopStatus=1');
			if ($fields == '') $fields = 's.shopTitle,s.shopId,s.shopImg';
			$db->field($fields);
			$db->limit($num)->where($where);
			$db->order('id desc');
			return $db->select();
		}
	}

	/**
	 * 获取搜索关键词
	 */
	public function listSearchkey($type, $cache = 0)
	{
		$cacheData = cache('TAG_SEARCHKEY_' . $type . "_" . $cache);
		if ($cacheData) return $cacheData;
		$keys = ZHTConf("CONF.hotWordsSearch");
		if ($keys != '') {
			$keys = explode(',', $keys);
			if ($type == 1) {
				foreach ($keys as $key => $v) {
					$keys[$key] = [];
					$keys[$key]['name'] = $v;
					$where = [];
					$where[] = ['dataFlag', '=', 1];
					$where[] = ['isSale', '=', 1];
					$where[] = ['goodsTitle', 'like', '%' . $v . '%'];
					$keys[$key]['count'] = Db::name('goods')->where($where)->count();
				}
			}
		}
		cache('TAG_SEARCHKEY_' . $type . "_" . $cache, $keys, $cache);
		return $keys;
	}

	/**
	 * 获取高评分商品
	 */
	public function listScore($catId, $num, $cache = 0)
	{
		$cacheData = cache('TAG_SCORE_' . $catId . "_" . $cache);
		if ($cacheData) return $cacheData;
		$scores = ZHTConf("CONF.hotWordsSearch");
		if ($scores != '') {
			$where = [];
			$where[] = ['serviceScore', '>=', 4];
			$where[] = ['g.dataFlag', '=', 1];
			$where[] = ['ga.dataFlag', '=', 1];
			$where[] = ['goodsScore', '>=', 4];
			$where[] = ['timeScore', '>=', 4];
			if ($catId > 0) $where[] = ['g.goodsCatIdPath', 'like', $catId . "\_%"];
			$scores = Db::name('goods')->alias('g')
				->field('g.goodsId,g.goodsImg,g.goodsTitle,g.shopPrice,ga.content,u.loginName,u.userName')
				->join('__GOODS_APPRAISES__ ga', 'g.goodsId=ga.goodsId', 'inner')
				->join('__USERS__ u', 'u.userId=ga.userId', 'inner')
				->where($where)
				->order('ga.create_time desc')
				->limit($num)
				->select();
		}
		cache('TAG_SCORE_' . $catId . "_" . $cache, $scores, $cache);
		return $scores;
	}

	/**
	 * 获取店铺分类列表
	 */
	public function listShopCats($parentId = 0, $num, $shopId = 0, $cache = 0){
		if ($shopId == 0) return [];
		$cacheData = cache('TAG_SHOP_CATS_' . $shopId . "_" . $parentId . "_" . $num . "_" . $cache);
		if ($cacheData) return $cacheData;
		$where = [];
		$where[] = ['isShow', '=', 1];
		$where[] = ['dataFlag', '=', 1];
		$where[] = ['shopId', '=', $shopId];
		$where[] = ['parentId', '=', $parentId];
		$data = Db::name('shop_goods_cat')
			->field('catId,shopId,catTitle')
			->limit($num)->where($where)
			->order('dataSort desc')
			->select();
		cache('TAG_SHOP_CATS_' . $shopId . "_" . $parentId . "_" . $num . "_" . $cache, $data, $cache, 'CACHETAG_' . $shopId);
		return $data;
	}

	/**
	 * 获取指定店铺商品
	 */
	public function listShopGoods($type, $shopId, $cat = 0, $num = 0, $cache = 0){
		$cacheData = cache('TAG_SHOP_GOODS_' . $type . "_" . $shopId . "_" . $cat . "_" . $num . "_" . $cache);
		if ($cacheData) return $cacheData;
		$types = ['recom' => 'isShopRecom', 'new' => 'isShopNew', 'hot' => 'isShopHot', 'best' => 'isShopBest'];
		$order = ['recom' => 'saleNum desc,goodsId asc', 'new' => 'saleTime desc,goodsId asc', 'hot' => 'saleNum desc,goodsId asc', 'best' => 'saleNum desc,goodsId asc'];
		if (!isset($types[$type])) return [];
		$where = [];
		if ($cat > 0) {
			$parentId = Db::name('shop_goods_cat')->where(['catId' => $cat, 'shopId' => $shopId, 'dataFlag' => 1, 'isShow' => 1])->value('parentId');
			if ($parentId > 0) {
				$where[] = ['shopGoodsCatIdPath', 'like', $parentId.'_'.$cat.'\_%'];
			} else {
				$where[] = ['shopGoodsCatIdPath', 'like', $cat.'\_%'];
			}
		}
		$where[] = ['g.shopId', '=', $shopId];
		$where[] = ['isSale', '=', 1];
		$where[] = ['goodsStatus', '=', 1];
		$where[] = ['dataFlag', '=', 1];
		// $where[] = [$types[$type], '=', 1];
		$goods = Db::name('goods')
			->alias('g')
			->join('__GOODS_SCORE__ gs', 'gs.goodsId = g.goodsId', 'left')
			->where($where)->field('g.goodsId,goodsTitle,goodsImg,goodsSn,goodsStock,saleNum,shopPrice,marketPrice,isSpec,appraiseNum,dataVisit,gs.totalScore,gs.totalUsers')
			->order($order[$type])->limit($num)->select();
		$ids = [];
		foreach ($goods as $key => $v) {
			if ($v['isSpec'] == 1) $ids[] = $v['goodsId'];
			$goods[$key]['praiseRate'] = ($v['totalScore'] > 0) ? (sprintf("%.2f", $v['totalScore'] / ($v['totalUsers'] * 15)) * 100) . '%' : '100%';
		}
		if (!empty($ids)) {
			$specs = [];
			$rs = Db::name('goods_spec gs ')->where([['goodsId', 'in', $ids], ['dataFlag', '=', 1]])->order('id')->select();
			foreach ($rs as $key => $v) {
				$specs[$v['goodsId']] = $v;
			}
			foreach ($goods as $key => $v) {
				if (isset($specs[$v['goodsId']]))
					$goods[$key]['specs'] = $specs[$v['goodsId']];
			}
		}
		cache('TAG_SHOP_GOODS_' . $type . "_" . $shopId . "_" . $cat . "_" . $num . "_" . $cache, $goods, $cache, 'CACHETAG_' . $shopId);
		return $goods;
	}
}
